import province from '#/tools';
export function getRulesForEditOption(editOption: any) {
  const rules: any = {};
  if (editOption.items && editOption.items.length > 0) {
    editOption.items.map((item: any) => {
      if (item.rules) {
        const l: any = [];
        l.push({
          required: item.rules.required,
          trigger: item.rules.trigger,
          message: item.rules.message,
        });
        if (item.rules.more?.length > 0) {
          item.rules.more?.map((m: any) => {
            l.push(m);
          });
        }
        rules[item.prop] = l;
      }
    });
  }
  return rules;
}
export function getProvinces(
  ...args: [string | string[], string?, string?, string?]
) {
  const { arrCode, type } = switchProvinces(...args);
  // 省
  let provinceItem: item | null;
  // 市
  let cityItem: item | null;
  // 区
  let countyItem: item | null;

  provinceItem =
    province.find((item: any) => item.value === arrCode[0]) || null;
  provinceItem
    ? (cityItem =
        provinceItem.children?.find((item: any) => item.value === arrCode[1]) ||
        null)
    : (cityItem = null);
  cityItem
    ? (countyItem =
        cityItem.children?.find((item: any) => item.value === arrCode[2]) ||
        null)
    : (countyItem = null);

  return type
    ? [provinceItem?.label, cityItem?.label, countyItem?.label].join(type)
    : [provinceItem?.label, cityItem?.label, countyItem?.label];
}
/**
 * 获取省市区name
 * ...args:剩余参数
 * */
interface item {
  label: string;
  value: string;
  children?: Array<item>;
}
function switchProvinces(
  ...args: [string | string[], string?, string?, string?]
) {
  // 省市区 code 数组
  let arrCode: any = [];
  // 以什么连接
  let type: string = '';
  switch (args.length) {
    case 1: // [code1,code2,code3] 省市区code数据
      arrCode = args[0];
      break;
    case 2: // [code1,code2,code3] 省市区code数据 type='/'
      arrCode = args[0];
      type = args[1] || '';
      break;
    case 3: // code1,code2,code3 省市区code数据
      arrCode = args;
      break;
    case 4: // code1,code2,code3 省市区code数据 type='/'
      arrCode = [args[0], args[1], args[2]];
      type = args[3] || '';
      break;
    default:
      return arrCode;
  }
  return {
    arrCode,
    type,
  };
}
/**
 * @description 生成唯一 uuid
 * @return string
 */
export function generateUUID() {
  if (typeof crypto === 'object') {
    if (typeof crypto.randomUUID === 'function') {
      return crypto.randomUUID();
    }
    if (
      typeof crypto.getRandomValues === 'function' &&
      typeof Uint8Array === 'function'
    ) {
      const callback = (c: any) => {
        const num = Number(c);
        return (
          num ^
          ((crypto.getRandomValues(new Uint8Array(1)[0] as any) &
            (15 >> (num / 4))) as any)
        ).toString(16);
      };
      return '10000000-1000-4000-8000-100000000000'.replace(/[018]/g, callback);
    }
  }
  let timestamp = new Date().getTime();
  let performanceNow =
    (typeof performance !== 'undefined' &&
      performance.now &&
      performance.now() * 1000) ||
    0;
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
    let random = Math.random() * 16;
    if (timestamp > 0) {
      random = (timestamp + random) % 16 | 0;
      timestamp = Math.floor(timestamp / 16);
    } else {
      random = (performanceNow + random) % 16 | 0;
      performanceNow = Math.floor(performanceNow / 16);
    }
    return (c === 'x' ? random : (random & 0x3) | 0x8).toString(16);
  });
}
export function getNameById(id: any, list: any, idKey: any, nameKey: any) {
  let n = '';
  idKey = idKey || 'id';
  nameKey = nameKey || 'name';
  list.find(function (obj: any) {
    if (
      typeof id !== 'undefined' &&
      typeof obj[idKey] !== 'undefined' &&
      obj[idKey].toString() === id.toString()
    ) {
      n = obj[nameKey];
    }
  });
  return n;
}
export function getIndexById(id: any, list: any, idKey: any) {
  for (let i = 0; i < list.length; i++) {
    if (list[i][idKey] === id) {
      return i;
    }
  }
  return -1; // 如果没有找到，返回-1
}
export function formatDateTime(t: any) {
  t = new Date(t);
  return (
    t.getFullYear() +
    '-' +
    (t.getMonth() + 1) +
    '-' +
    t.getDate() +
    ' ' +
    t.getHours() +
    ':' +
    t.getMinutes() +
    ':' +
    t.getSeconds()
  );
}
export function formatDate(t: any) {
  if (typeof t !== 'undefined' && t !== '') {
    t = new Date(t);
    return (
      t.getFullYear() +
      '-' +
      (t.getMonth() + 1).toString().padStart(2, '0') +
      '-' +
      t.getDate().toString().padStart(2, '0')
    );
  }
}
